package com.sh.controller;

import com.sh.common.BaseResult;
import com.sh.common.enums.BizCodeEnum;

import com.sh.common.pojo.query.OperatorPageQuery;
import com.sh.entity.Operator;

import com.sh.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;


@Slf4j
@RestController
@RequestMapping("/operator")
public class OperatorController {

    @Autowired
    private IOperatorService operatorService;

    @PostMapping("/page")
    public BaseResult page(@Valid @RequestBody OperatorPageQuery query) {
        return BaseResult.success(operatorService.queryOperatorPage(query));
    }

    @GetMapping("/{id}")
    public BaseResult getById(@PathVariable("id") Long id) {
        Operator operator = operatorService.getById(id);
        if (operator == null) {
            return BaseResult.fail(BizCodeEnum.DATA_NOT_EXISTED);
        }
        return BaseResult.success(operatorService.transferToDto(operator));
    }

    @PostMapping("/tree")
    public BaseResult getOperatorTree(@RequestBody OperatorPageQuery query) {
        return BaseResult.success(operatorService.queryOperatorTree(query));
    }

}
